/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package azhar.com;

import azhar.view.control.CreateMessage;
import azhar.view.*;
import azhar.view.control.AddGuru;
import azhar.view.control.AddKaryawan;
import azhar.view.control.AddSiswa;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.*;

/**
 *
 * @author Al-Insan
 */
public class PopupMenuMessage extends JPopupMenu {

    private JMenuItem SEND_MESSAGE = new JMenuItem("Send");
    private JMenuItem SEND_AGAIN = new JMenuItem("Resend");
    private JMenuItem DELETE_MESSAGE = new JMenuItem("Delete Message");
    private JMenuItem FORWARD_MESSAGE = new JMenuItem("Forward Message");
    private JMenuItem REPLY_MESSAGE = new JMenuItem("Reply Message");
    private JMenuItem EDIT_MESSAGE = new JMenuItem("Edit Message");
    private JMenu SAVE_NUMBER = new JMenu("Save Number");
    private JMenuItem SAVE_GURU = new JMenuItem("Save to guru");
    private JMenuItem SAVE_SISWA = new JMenuItem("Save to siswa");
    private JMenuItem SAVE_KARYAWAN = new JMenuItem("Save to karyawan");
    private MouseAdapter[] mouse = new MouseAdapter[9];
    private TableOperation tabOp;
    private Main main;
    private azhar.view.Messages msg;
    public JTable table;

    public PopupMenuMessage(JTable t, Main m, azhar.view.Messages ms) {
        tabOp = new TableOperation();
        table = t;
        main = m;
        msg = ms;
        setMenu();
    }

    private void createMenu() {
        add(REPLY_MESSAGE);
        add(FORWARD_MESSAGE);
        add(EDIT_MESSAGE);
        add(DELETE_MESSAGE);
        add(new JSeparator(JSeparator.HORIZONTAL));
        add(SEND_MESSAGE);
        add(SEND_AGAIN);
        add(SAVE_NUMBER);
        SAVE_NUMBER.add(SAVE_GURU);
        SAVE_NUMBER.add(SAVE_SISWA);
        SAVE_NUMBER.add(SAVE_KARYAWAN);
    }

    private void setEnableAll() {
        setAllAction();
        if (table.getSelectedRowCount() < 2) {
            REPLY_MESSAGE.setEnabled(true);
            FORWARD_MESSAGE.setEnabled(true);
            EDIT_MESSAGE.setEnabled(true);
            SAVE_NUMBER.setEnabled(true);
            SAVE_GURU.setEnabled(true);
            SAVE_SISWA.setEnabled(true);
            SAVE_KARYAWAN.setEnabled(true);
        } else {
            REPLY_MESSAGE.setEnabled(false);
            FORWARD_MESSAGE.setEnabled(false);
            EDIT_MESSAGE.setEnabled(false);
            SAVE_NUMBER.setEnabled(false);
            REPLY_MESSAGE.removeMouseListener(mouse[2]);
            FORWARD_MESSAGE.removeMouseListener(mouse[4]);
            EDIT_MESSAGE.removeMouseListener(mouse[5]);
        }
        DELETE_MESSAGE.setEnabled(true);
    }

    private void setAllAction() {
        mouse[0] = new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                try {
                    ArrayList<azhar.com.Messages> messages = new ArrayList<azhar.com.Messages>();
                    for (int i = 0; i < table.getSelectedRows().length; i++) {
                        String number = (String) table.getValueAt(table.getSelectedRows()[i], 0);
                        String text = (String) table.getValueAt(table.getSelectedRows()[i], 1);
                        messages.add(new Messages(number, text));
                        System.out.println(number + text);
                    }
                    main.sendMessage(messages);
                    messages.clear();
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(main, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
                }
            }
        };
        mouse[1] = new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                try {
                    ArrayList<azhar.com.Messages> messages = new ArrayList<azhar.com.Messages>();
                    for (int i = table.getSelectedRows().length - 1; i > -1; i--) {
                        messages.add(new azhar.com.Messages("" + table.getValueAt(table.getSelectedRows()[i], 0), "" + table.getValueAt(table.getSelectedRows()[i], 1)));
//                        System.out.println(messages.get(i).getNumber() + messages.get(i).getText());
                        msg.deleteData(table.getAccessibleContext().getAccessibleName(), table.getSelectedRows()[i]);
                    }
                    main.sendMessage(messages);
                    tabOp.deleteData(table, table.getSelectedRows());
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(main, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
                }
            }
        };
        mouse[2] = new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                String number = new Separate().separated(tabOp.getValues(table, "Sender"));
                new CreateMessage(main, true, number).setVisible(true);
            }
        };
        mouse[3] = new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                for (int i = table.getSelectedRows().length - 1; i >= 0; i--) {
                    msg.deleteData(table.getAccessibleContext().getAccessibleName(), table.getSelectedRows()[i]);
                }
                tabOp.deleteData(table, table.getSelectedRows());
                msg.clearText();
            }
        };
        mouse[4] = new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                new CreateMessage(main, false, "", table.getValueAt(table.getSelectedRow(), getColumnIndex("Message"))).setVisible(true);
            }
        };
        mouse[5] = new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                new CreateMessage(main, true, "", table.getValueAt(table.getSelectedRow(), getColumnIndex("Message"))).setVisible(true);
            }
        };
        mouse[6] = new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                new AddGuru(main, true, table.getValueAt(table.getSelectedRow(), 0)).setVisible(true);
            }
        };
        mouse[7] = new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                new AddKaryawan(main, true, table.getValueAt(table.getSelectedRow(), 0)).setVisible(true);
            }
        };
        mouse[8] = new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                new AddSiswa(main, true, table.getValueAt(table.getSelectedRow(), 0)).setVisible(true);
            }
        };
        SEND_MESSAGE.addMouseListener(mouse[0]);
        SEND_AGAIN.addMouseListener(mouse[1]);
        REPLY_MESSAGE.addMouseListener(mouse[2]);
        DELETE_MESSAGE.addMouseListener(mouse[3]);
        FORWARD_MESSAGE.addMouseListener(mouse[4]);
        EDIT_MESSAGE.addMouseListener(mouse[5]);
        SAVE_GURU.addMouseListener(mouse[6]);
        SAVE_KARYAWAN.addMouseListener(mouse[7]);
        SAVE_SISWA.addMouseListener(mouse[8]);
    }

    public void setMenu() {
        createMenu();
        setEnableAll();
        if (table.getAccessibleContext().getAccessibleName().equals("inbox")) {
            menuInbox();
        } else if (table.getAccessibleContext().getAccessibleName().equals("draft")) {
            menuDraft();
        } else if (table.getAccessibleContext().getAccessibleName().equals("outbox")) {
            menuOutbox();
        } else if (table.getAccessibleContext().getAccessibleName().equals("sentItem")) {
            menuSentItem();
        }
    }

    private void menuInbox() {
        SEND_MESSAGE.setEnabled(false);
        SEND_MESSAGE.removeMouseListener(mouse[0]);
        SEND_AGAIN.setEnabled(false);
        SEND_AGAIN.removeMouseListener(mouse[1]);
    }

    private void menuDraft() {
        SEND_AGAIN.setEnabled(false);
        SEND_AGAIN.removeMouseListener(mouse[1]);
        REPLY_MESSAGE.setEnabled(false);
        REPLY_MESSAGE.removeMouseListener(mouse[2]);
        FORWARD_MESSAGE.setEnabled(false);
        FORWARD_MESSAGE.removeMouseListener(mouse[4]);
    }

    private void menuOutbox() {
        REPLY_MESSAGE.setEnabled(false);
        REPLY_MESSAGE.removeMouseListener(mouse[2]);
        FORWARD_MESSAGE.setEnabled(false);
        FORWARD_MESSAGE.removeMouseListener(mouse[4]);
        SEND_MESSAGE.setEnabled(false);
        SEND_MESSAGE.removeMouseListener(mouse[0]);
    }

    private void menuSentItem() {
        REPLY_MESSAGE.setEnabled(false);
        REPLY_MESSAGE.removeMouseListener(mouse[2]);
        SEND_MESSAGE.setEnabled(false);
        SEND_MESSAGE.removeMouseListener(mouse[0]);
        SEND_AGAIN.setEnabled(false);
        SEND_AGAIN.removeMouseListener(mouse[1]);
    }

    public int getColumnIndex(String name) {
        int index = 0;
        for (int i = 0; i < table.getColumnCount(); i++) {
            index = i;
            if (table.getColumnName(i).equals(name)) {
                break;
            }
        }
        return index;
    }
}
